package com.findMoneyGame.server {
	import flash.events.Event;
	import flash.events.SecurityErrorEvent;
	import flash.events.IOErrorEvent;
	import flash.events.EventDispatcher;
	import flash.display.Loader;
	import flash.display.Bitmap;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLLoader;
	import flash.net.URLVariables;

	public class ServerBridge {
		private var imageLoader : Loader = new Loader();
		private var urlLoader : URLLoader;
		
		public function get imageBytesTotal () : int { return imageLoader.contentLoaderInfo.bytesTotal; }
		public function get imageBytesLoaded () : int { return imageLoader.contentLoaderInfo.bytesLoaded; }
		public function get urlRequestBytesTotal () : int { return urlLoader.bytesTotal; }
		public function get urlRequestBytesLoaded () : int { return urlLoader.bytesLoaded; }

		public function beginLoadImage (imageUrl : String, complete : Function, error : Function) : void {
			trace('ServerBridge.beginLoadImage() Url: ' + imageUrl);
			var unsubscribeLoadImage : Function;
			var loadImageComplete = function (e : Event) {
				unsubscribeLoadImage();
				complete(imageLoader.content as Bitmap);
			};
			var loadImageError = function (e : Event) {
				unsubscribeLoadImage();
				error(e);
			};
			unsubscribeLoadImage = function () {
				imageLoader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, loadImageError);
				imageLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, loadImageError);
				imageLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadImageComplete);
			};
			imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadImageComplete);
			imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loadImageError);
			imageLoader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loadImageError);
			imageLoader.load(new URLRequest(imageUrl));
		}
		public function beginGetUrlRequest (url : String, complete : Function, error : Function) : void {
			trace('ServerBridge.beginGetUrlRequest() Url: ' + url);
			var unsubscribe : Function;
			var reqComplete = function (e : Event) {
				unsubscribe();
				complete(urlLoader.data);
			};
			var reqError = function (e : Event) {
				unsubscribe();
				error(e);
			};
			unsubscribe = function () {
				urlLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, reqError);
				urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, reqError);
				urlLoader.removeEventListener(Event.COMPLETE, reqComplete);
			};
			urlLoader = new URLLoader();
			urlLoader.addEventListener(Event.COMPLETE, reqComplete);
			urlLoader.addEventListener(IOErrorEvent.IO_ERROR, reqError);
			urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, reqError);
			urlLoader.load(new URLRequest(url));
		}
		public function beginPostUrlRequest (url : String, vars : URLVariables, complete : Function, error : Function) {
			trace('ServerBridge.beginPostUrlRequest() Url: ' + url + ' Vars: ' + vars);
			var unsubscribe : Function;
			var reqComplete = function (e : Event) {
				unsubscribe();
				complete(urlLoader.data);
			};
			var reqError = function (e : Event) {
				unsubscribe();
				error(e);
			};
			unsubscribe = function () {
				urlLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, reqError);
				urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, reqError);
				urlLoader.removeEventListener(Event.COMPLETE, reqComplete);
			};
			var request = new URLRequest(url);
			request.method = URLRequestMethod.POST;
			request.data = vars;
			urlLoader = new URLLoader();
			urlLoader.addEventListener(Event.COMPLETE, reqComplete);
			urlLoader.addEventListener(IOErrorEvent.IO_ERROR, reqError);
			urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, reqError);
			urlLoader.load(request);
		}
	}
}